﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using ClassLibrary.Entity;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblLayoutCodeDAO
    {
        private DBService _db = null;
        public TblLayoutCodeDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }

        public bool InsUpdLayoutCode(TblLayoutCode LayoutCode, int mode)
        {
            bool flag = false;
            try {
                _db.ExeSP_NonQuery("sp_TblLayoutCode_InsUpd",
                    new string[] { "@IDLayout", "@LayoutName", "@ImageURL", "@CodeDetails", "@ViewCount", "@Display", "@IdLanguage", "@mode" },
                    new object[] { LayoutCode.IDLayout, LayoutCode.LayoutName, LayoutCode.ImageURL, LayoutCode.CodeDetails, LayoutCode.ViewCount,
                                   LayoutCode.Display, LayoutCode.IdLanguage, mode},
                    CommandType.StoredProcedure);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        public TblLayoutCode GetLayoutByID(int IDLayout)
        {
            TblLayoutCode Layout = new TblLayoutCode();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("select * from TblLayoutCode where IDLayout = @IDLayout",
                    new string[] { "@IDLayout" },
                    new object[] { IDLayout }
                    , CommandType.Text);
                foreach (DataRow dr in dt.Rows)
                {
                    Layout.IDLayout = IDLayout;
                    Layout.LayoutName = dr["LayoutName"].ToString();
                    Layout.ImageURL = dr["ImageURL"].ToString();
                    Layout.CodeDetails = dr["CodeDetails"].ToString();
                    Layout.Display = Convert.ToBoolean(dr["Display"]);
                    Layout.ViewCount = Convert.ToInt32(dr["ViewCount"]);
                    Layout.IdLanguage = Convert.ToInt32(dr["IdLanguage"]);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return Layout;
        }

        public DataTable ListLayout()
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblLayoutCode order by IDLayout desc", false, CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

    }
}
